#coding:utf-8
#!/usr/bin/python
import urllib
import codecs
from BeautifulSoup import BeautifulSoup
from sys import argv
import re,time
from colorama import init
import platform

class bcolors:
    BOLD = '\033[1m'
    underline='\033[4m'
    blink='\033[5m'
    reverse='\033[7m'
    concealed='\033[8m'
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'

    def disable(self):
        self.HEADER = ''
        self.OKBLUE = ''
        self.OKGREEN = ''
        self.WARNING = ''
        self.FAIL = ''
        self.ENDC = ''

class Translate:
    def Start(self):
        self._get_html_sourse()
        self._get_content()
        self.parse_li_dct_ec(self.htmlsourse)

    def _get_html_sourse(self):
        word=argv[1] if len(argv)>1 else ''
        url = 'http://www.google.com/dictionary?aq=f&langpair=en|zh-CN&q=' + word + '&hl=en'
        self.htmlsourse=unicode(urllib.urlopen(url).read(),"utf-8","ignore").encode("utf-8","ignore")

    def parse_li_dct_ec(self,str1):
        soup=BeautifulSoup(str1)
        dct_eh_strs = soup.findAll("li",{"class":"dct-eh"})
        if len(dct_eh_strs):
            for each in dct_eh_strs:  
                div_dct_eh_str = soup.find("div",{"class":"dct-eh"})
                if div_dct_eh_str:
                    span_class_dct_tps = div_dct_eh_str.findAll("span",{"class":"dct-tp"})
                    for each_dct_tp in span_class_dct_tps:
                        tmp_tp = self._remove_tag(each_dct_tp)
                        print tmp_tp.encode("utf-8","ignore")
                dct_ec_strs = each.findAll("li",{"class":"dct-ec"})
                if len(dct_ec_strs):
                    for each1 in dct_ec_strs:
                        self.parse_span_partofspeech(str(each1))
                else:
                    self.parse_div_dct_em(each)
        else:
            if platform.system() == 'Windows':
                print self._remove_tag(soup).encode("gbk")
            else:
                print self._remove_tag(soup)
            
        
    def parse_span_partofspeech(self,str2):
        soup1=BeautifulSoup(str2)
        part_of_speechs = soup1.find("span",{"title":"Part-of-Speech"})
        tmp = self._remove_tag(part_of_speechs)
        print "\033[32m" + tmp + bcolors.ENDC
        self.parse_div_dct_em(soup1)
    
    def parse_div_dct_em(self,soup):
        div_dct_ems = soup.findAll("div",{"class":"dct-em"})
        for each in div_dct_ems:
            #print type(self._remove_tag(each))
            if platform.system() == 'Windows':
                print self._remove_tag(each).encode("gbk")
            else:
                print self._remove_tag(each)

    def _get_content(self):
        root =  re.search(r'[<]div\s*?class[=]["]dct-srch-rslt["][>].*[<][/]div[>]',self.htmlsourse,re.S)
        if root:
            self.htmlsourse = root.group()
            #print root.group()
            pass

    def _remove_tag(self,tag):
        return ''.join([element  for element in tag.recursiveChildGenerator() if isinstance(element,unicode)])



if __name__=="__main__":
    init()
    Translate().Start()
